<?php
class DataUtil{
	static function getCategorys($db){
		$catTable = new BaseDao($db,'goods_category','cat_id');
		$categorys = $catTable->getAll(null, null,array('is_show desc','sort_order asc'));
		$newcategorys = array();
		foreach ($categorys as $category0){
			if($category0['parent_id'] == 0){
				$childs0 = array();
				foreach ($categorys as $category1){
					if($category0['cat_id'] == $category1['parent_id']){
						$childs1 = array();
						foreach ($categorys as $category2){
							if($category1['cat_id'] == $category2['parent_id']){
								$childs1[] = $category2;
							}
						}
						$category1['childs'] = $childs1;
						$childs0[] = $category1;
					}
				}
				$category0['childs'] = $childs0;
				$newcategorys[] = $category0;
			}
		}
		return $newcategorys;
	}
	
	static function getCategoryMap($db){
		$catTable = new BaseDao($db,'goods_category','cat_id');
		$categorys = $catTable->getAll(null, null,array('is_show desc','sort_order asc'));
	
		$newcategorys = array();
		foreach ($categorys as $category){
			$newcategorys[$category['cat_id']] = $category;
		}
		return $newcategorys;
	}
	
	
	static function getLeafCategory($categoryId,$categorys,&$parentCategory=null,&$rootCategory=null,&$categoryList=null){
		foreach ($categorys as $cat1){
			foreach ($cat1['childs'] as $cat2){
				if($categoryId == $cat2['cat_id']){
					$rootCategory = $cat1;
					$parentCategory = $cat2;
					$categoryList =  $cat2['childs'];
					return null;
				}else{
					foreach ($cat2['childs'] as $cat3){
						if($categoryId == $cat3['cat_id']){
							$rootCategory = $cat1;
							$parentCategory = $cat2;
							$categoryList =  $cat2['childs'];
							return $cat3;
						}
					}
				}
			}
		}
		return null;
	}
	
	
	
}
?>